package com.fruit.controller.wap.transaction;


import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.pay.TransactionBankFlow;
import com.fruit.pojo.pay.TransactionFlow;
import com.fruit.pojo.user.SysUser;
import com.fruit.service.pay.PayManager;
import com.fruit.service.user.SysRoleManager;
import com.fruit.util.DateUtil;


@Controller
@RequestMapping(value = "/wap/transaction/")
public class TransactionController extends BaseController {
	
	@Autowired
	private PayManager payService;
	
	@Autowired
	private SysRoleManager sysRoleService;

	@RequestMapping(value = "/list",method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView list(TransactionFlow t,HttpSession session){
		ModelAndView view = this.getModelAndView();
		//SysUser user = (SysUser) session.getAttribute("sysUser");
		SysUser user =new SysUser();
		user.setRoleId("1");
		user.setUserId("1");
		 long time= System.currentTimeMillis();
		if(sysRoleService.getQxNameCnt(user.getRoleId(),"transaction_view")){
			if(t !=null){
				t.setStart((t.getPage() - 1) * t.getShowNumber());
			}
			List<TransactionFlow> tf=null;
			if(sysRoleService.getQxNameCnt(user.getRoleId(),"all_view")){
				tf =payService.getTransactionFlowList(t);
			}else{
				t.setBinding(user.getUserId());
				tf = payService.getTransactionFlowList(t);
			}
			if(t.getKeyword() != null){
				view.addObject("keyword",t.getKeyword());
			}
			view.addObject("tfList",tf);
			if(tf != null){
				view.addObject("size",tf.size());
				view.addObject("msg","00");
			}else{
				view.addObject("msg","未找到数据!");
			}
		}else{
			view.addObject("msg","您无权查看!");
		}
		view.addObject("time",time);
		view.setViewName("transaction/finance_list");
		return view;
	}
	
	@RequestMapping(value = "/ajaxList",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> ajaxList(TransactionFlow t,HttpSession session){
		Map<String,Object> resMap = new HashMap<String, Object>();
		SysUser user = (SysUser) session.getAttribute("sysUser");
		if(sysRoleService.getQxNameCnt(user.getRoleId(),"transaction_view")){
			if(t != null){
				t.setStart((t.getPage() - 1) * t.getShowNumber());
			}
			List<TransactionFlow> tf = payService.getTransactionFlowList(t);
			if(tf!=null){
				resMap.put("tfList", tf);
				resMap.put("size",tf.size());
			}else{
				resMap.put("tfList", tf);
				resMap.put("size",0);
			}
		}else{
			resMap.put("msg","你无权查看!");
		}
		return resMap;
	}
	
	
	@RequestMapping(value = "/toVoucher",method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView toVoucher(String id,HttpSession session){
		ModelAndView view = this.getModelAndView();
		//SysUser user = (SysUser) session.getAttribute("sysUser");
		SysUser user =new SysUser();
		user.setRoleId("1");
		user.setUserId("1");
		 long time= System.currentTimeMillis();
		if(sysRoleService.getQxNameCnt(user.getRoleId(),"transaction_view")){
			TransactionFlow tf = payService.getTransactionFlow(id);
			if(tf != null){
				view.addObject("tf",tf);
			}
			List<TransactionBankFlow> tbf = payService.getTransactionBankFlowList(id);
			view.addObject("tbf",tbf);
			view.addObject("time",time);
		}else{
			view.addObject("msg","你无权查看!");
		}
		view.setViewName("transaction/pay_detail");
		return view;
	}
	
	@RequestMapping(value = "/toProof",method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView toProof(String id,String state,HttpSession session){
		ModelAndView view = this.getModelAndView();
			//SysUser user = (SysUser) session.getAttribute("sysUser");
			SysUser user =new SysUser();
			user.setRoleId("1");
			user.setUserId("1");
			if(sysRoleService.getQxNameCnt(user.getRoleId(),"transaction_save")){
			TransactionFlow tf = payService.getTransactionFlow(id);
			BigDecimal price=new BigDecimal("0.00");
			List<TransactionBankFlow> tbf = payService.getTransactionBankFlowList(id);
			if(tbf != null){
				for (TransactionBankFlow flow : tbf) {
					price =price.add(flow.getPrice());
				}
			}
			if(tf != null){
				BigDecimal totalPrice=tf.getTotalPrice().subtract(price);
				tf.setTotalPrice(totalPrice);
			}
			view.addObject("id",id);
			view.addObject("tf",tf);
			if(state != null){
				view.addObject("state",state);
			}
		}else{
			view.addObject("msg","你无权添加支付流水!");
		}
		view.setViewName("transaction/proof");
		return view;
	}
	
	@RequestMapping(value="/proof",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> proof(@RequestParam("instrumen")MultipartFile instrumen,TransactionBankFlow t,HttpSession session){
		Map<String,Object> map=new HashMap<>();
		//SysUser user = (SysUser) session.getAttribute("sysUser");
		SysUser user =new SysUser();
		user.setRoleId("1");
		user.setUserId("1");
		if(sysRoleService.getQxNameCnt(user.getRoleId(),"transaction_save")){
			int num=payService.saveBankFlow(instrumen,t,user);
			map.put("id", t.getTransactionId());
			if(num ==0){
				map.put("state", "00");
			}else if(num==2){
				map.put("state", "01");
			}else if(num==3){
				map.put("state", "02");
			}else{
				map.put("state", "03");
			}
		}else{
			map.put("state", "04");
		}
		return map;
	}
}
